<?php

/*
|--------------------------------------------------------------------------
| Application Events
|--------------------------------------------------------------------------
|
*/
//用户行为记录
Event::listen('behavior.student',   function($monitorParam)
{
    /*参数结构
    */
//    $monitorParam = new stdClass();
//    $monitorParam->user_id      = null;
//    $monitorParam->item_id      = null;          //
//    $monitorParam->item_type    = null;          //记录用户的行为对象类型，题目，视频等等
//    $monitorParam->content_type = null;
//    $monitorParam->content      = null;
//    $monitorParam->page         = null;          //记录用户在哪个页面执行的操作．
//    $monitorParam->msg          = null;
//    $monitorParam->state        = null;          //执行状态结果
    // 根据请求的接口，记录用户行为
    $request = explode( '/' , Request::path() );
    $actionCode = BehaviorCodeEnum::$caCode[$request[1]][$request[2]];
    //记录的数据库log表中
    $sql="INSERT INTO `Udo_log`
 		(
 		`user_id`,
 		`resource_id`,
 		`resource_type`,
 		`action_id`,
 		`page`,
 		`content_type`,
 		`content`,
 		`msg`,
 		`state`,`create_time`) VALUES
 		('".
        $monitorParam->stuff_id      ."','".
        $monitorParam->item_id      ."','".
        $monitorParam->item_type    ."','".
        $actionCode                 ."','".
        $monitorParam->page         ."','".
        $monitorParam->content_type ."','".
        $monitorParam->content      ."','".
        $monitorParam->msg          ."','".
        $monitorParam->state        ."',NOW())";
    return DB::insert($sql);
});
//推送
Event::listen('push.onlyPush',      function($pushParam){

//    参数结构
//    $pushParam = new stdClass();
//    $pushParam->stuff_id  = '０';//推送对象
//    $pushParam->type      = MessageCodeEnum::MESSAGE_STU_NEW_HOMEWORK;//消息类型，见Constants/MessageCodeEnum.php
//    $pushParam->withIgt   = true;//是否透传
    (property_exists($pushParam, 'withIgt'))?$pushParam->withIgt = $pushParam->withIgt:$pushParam->withIgt = true;

    if(!property_exists($pushParam, 'stuff_id') || !property_exists($pushParam, 'type'))
        return false;
    $device_info = DB::table('plant')->where('stuff_id','=',$pushParam->stuff_id)->first();
    if ($device_info) {
        $pushConf = Config::get("push");
        Push::instance()->setAttribute($pushConf);

        $target = array(
            "type"          => $device_info->os_type,
            "ClientID"      => $device_info->client_id,
            "DeviceToken"   => $device_info->device_token,
        );
        $content = Lang::get('push.' . $pushParam->type);
        //dd($pushParam);
        Push::instance()->pushToSingle($target, $content);//推送
        if ($device_info->os_type == 1) {
        	if($pushParam->withIgt) Push::instance()->IGtTransmission($content['info'], $device_info->client_id);
        }
    }
});
//系统消息
Event::listen('message.base',       function($messageParam){
//    参数结构
//    $pushParam = new stdClass();
//    $pushParam->stuff_id  = '０';//推送对象
//    $pushParam->argv      = argv;//推送参数
//    $pushParam->type      = MessageCodeEnum::MESSAGE_STU_NEW_HOMEWORK;//消息类型，见Constants/MessageCodeEnum.php
//    $pushParam->sender    = sender;//发送者
//    $pushParam->receiver  = receiver;//接受者
    //写入到消息表中
   /*  if ($messageParam->type == 3){
        return true;
    } */
    $aData = array(
        'type'          => $messageParam->type,
        'argv'          => $messageParam->argv,
        'desc'          => Lang::get('push.' . $messageParam->type)['text'],
        'sender'        => $messageParam->sender,
        'receiver'      => $messageParam->receiver,
        'create_time'   => date("Y-m-d H:i:s", time())
    );
    DB::table('message')->insert($aData);
});
//带有消息的推送
Event::listen('push.withMessage',   function($pushAndMessageParam){
    Event::fire('push.onlyPush',$pushAndMessageParam);
    Event::fire('message.base',$pushAndMessageParam);
});